low_level.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. """
  2. Low-level helpers for the SecureTransport bindings.
  3. These are Python functions that are not directly related to the high-level APIs
  4. but are necessary to get them to work. They include a whole bunch of low-level
  5. CoreFoundation messing about and memory management. The concerns in this module
  6. are almost entirely about trying to avoid memory leaks and providing
  7. appropriate and useful assistance to the higher-level code.
  8. """
  9. import base64
  10. import ctypes
  11. import itertools
  12. import re
  13. import os
  14. import ssl
  15. import tempfile
  16. from .bindings import Security, CoreFoundation, CFConst
  17. # This regular expression is used to grab PEM data out of a PEM bundle.
  18. _PEM_CERTS_RE = re.compile(
  19. b"-----BEGIN CERTIFICATE-----\n(.*?)\n-----END CERTIFICATE-----", re.DOTALL
  20. )
  21. def _cf_data_from_bytes(bytestring):
  22. """
  23. Given a bytestring, create a CFData object from it. This CFData object must
  24. be CFReleased by the caller.
  25. """
  26. return CoreFoundation.CFDataCreate(
  27. CoreFoundation.kCFAllocatorDefault, bytestring, len(bytestring)
  28. )
  29. def _cf_dictionary_from_tuples(tuples):
  30. """
  31. Given a list of Python tuples, create an associated CFDictionary.
  32. """
  33. dictionary_size = len(tuples)
  34. # We need to get the dictionary keys and values out in the same order.
  35. keys = (t[0] for t in tuples)
  36. values = (t[1] for t in tuples)
  37. cf_keys = (CoreFoundation.CFTypeRef * dictionary_size)(*keys)
  38. cf_values = (CoreFoundation.CFTypeRef * dictionary_size)(*values)
  39. return CoreFoundation.CFDictionaryCreate(
  40. CoreFoundation.kCFAllocatorDefault,
  41. cf_keys,
  42. cf_values,
  43. dictionary_size,
  44. CoreFoundation.kCFTypeDictionaryKeyCallBacks,
  45. CoreFoundation.kCFTypeDictionaryValueCallBacks,
  46. )
  47. def _cf_string_to_unicode(value):
  48. """
  49. Creates a Unicode string from a CFString object. Used entirely for error
  50. reporting.
  51. Yes, it annoys me quite a lot that this function is this complex.
  52. """
  53. value_as_void_p = ctypes.cast(value, ctypes.POINTER(ctypes.c_void_p))
  54. string = CoreFoundation.CFStringGetCStringPtr(
  55. value_as_void_p, CFConst.kCFStringEncodingUTF8
  56. )
  57. if string is None:
  58. buffer = ctypes.create_string_buffer(1024)
  59. result = CoreFoundation.CFStringGetCString(
  60. value_as_void_p, buffer, 1024, CFConst.kCFStringEncodingUTF8
  61. )
  62. if not result:
  63. raise OSError("Error copying C string from CFStringRef")
  64. string = buffer.value
  65. if string is not None:
  66. string = string.decode("utf-8")
  67. return string
  68. def _assert_no_error(error, exception_class=None):
  69. """
  70. Checks the return code and throws an exception if there is an error to
  71. report
  72. """
  73. if error == 0:
  74. return
  75. cf_error_string = Security.SecCopyErrorMessageString(error, None)
  76. output = _cf_string_to_unicode(cf_error_string)
  77. CoreFoundation.CFRelease(cf_error_string)
  78. if output is None or output == u"":
  79. output = u"OSStatus %s" % error
  80. if exception_class is None:
  81. exception_class = ssl.SSLError
  82. raise exception_class(output)
  83. def _cert_array_from_pem(pem_bundle):
  84. """
  85. Given a bundle of certs in PEM format, turns them into a CFArray of certs
  86. that can be used to validate a cert chain.
  87. """
  88. # Normalize the PEM bundle's line endings.
  89. pem_bundle = pem_bundle.replace(b"\r\n", b"\n")
  90. der_certs = [
  91. base64.b64decode(match.group(1)) for match in _PEM_CERTS_RE.finditer(pem_bundle)
  92. ]
  93. if not der_certs:
  94. raise ssl.SSLError("No root certificates specified")
  95. cert_array = CoreFoundation.CFArrayCreateMutable(
  96. CoreFoundation.kCFAllocatorDefault,
  97. 0,
  98. ctypes.byref(CoreFoundation.kCFTypeArrayCallBacks),
  99. )
  100. if not cert_array:
  101. raise ssl.SSLError("Unable to allocate memory!")
  102. try:
  103. for der_bytes in der_certs:
  104. certdata = _cf_data_from_bytes(der_bytes)
  105. if not certdata:
  106. raise ssl.SSLError("Unable to allocate memory!")
  107. cert = Security.SecCertificateCreateWithData(
  108. CoreFoundation.kCFAllocatorDefault, certdata
  109. )
  110. CoreFoundation.CFRelease(certdata)
  111. if not cert:
  112. raise ssl.SSLError("Unable to build cert object!")
  113. CoreFoundation.CFArrayAppendValue(cert_array, cert)
  114. CoreFoundation.CFRelease(cert)
  115. except Exception:
  116. # We need to free the array before the exception bubbles further.
  117. # We only want to do that if an error occurs: otherwise, the caller
  118. # should free.
  119. CoreFoundation.CFRelease(cert_array)
  120. return cert_array
  121. def _is_cert(item):
  122. """
  123. Returns True if a given CFTypeRef is a certificate.
  124. """
  125. expected = Security.SecCertificateGetTypeID()
  126. return CoreFoundation.CFGetTypeID(item) == expected
  127. def _is_identity(item):
  128. """
  129. Returns True if a given CFTypeRef is an identity.
  130. """
  131. expected = Security.SecIdentityGetTypeID()
  132. return CoreFoundation.CFGetTypeID(item) == expected
  133. def _temporary_keychain():
  134. """
  135. This function creates a temporary Mac keychain that we can use to work with
  136. credentials. This keychain uses a one-time password and a temporary file to
  137. store the data. We expect to have one keychain per socket. The returned
  138. SecKeychainRef must be freed by the caller, including calling
  139. SecKeychainDelete.
  140. Returns a tuple of the SecKeychainRef and the path to the temporary
  141. directory that contains it.
  142. """
  143. # Unfortunately, SecKeychainCreate requires a path to a keychain. This
  144. # means we cannot use mkstemp to use a generic temporary file. Instead,
  145. # we're going to create a temporary directory and a filename to use there.
  146. # This filename will be 8 random bytes expanded into base64. We also need
  147. # some random bytes to password-protect the keychain we're creating, so we
  148. # ask for 40 random bytes.
  149. random_bytes = os.urandom(40)
  150. filename = base64.b16encode(random_bytes[:8]).decode("utf-8")
  151. password = base64.b16encode(random_bytes[8:]) # Must be valid UTF-8
  152. tempdirectory = tempfile.mkdtemp()
  153. keychain_path = os.path.join(tempdirectory, filename).encode("utf-8")
  154. # We now want to create the keychain itself.
  155. keychain = Security.SecKeychainRef()
  156. status = Security.SecKeychainCreate(
  157. keychain_path, len(password), password, False, None, ctypes.byref(keychain)
  158. )
  159. _assert_no_error(status)
  160. # Having created the keychain, we want to pass it off to the caller.
  161. return keychain, tempdirectory
  162. def _load_items_from_file(keychain, path):
  163. """
  164. Given a single file, loads all the trust objects from it into arrays and
  165. the keychain.
  166. Returns a tuple of lists: the first list is a list of identities, the
  167. second a list of certs.
  168. """
  169. certificates = []
  170. identities = []
  171. result_array = None
  172. with open(path, "rb") as f:
  173. raw_filedata = f.read()
  174. try:
  175. filedata = CoreFoundation.CFDataCreate(
  176. CoreFoundation.kCFAllocatorDefault, raw_filedata, len(raw_filedata)
  177. )
  178. result_array = CoreFoundation.CFArrayRef()
  179. result = Security.SecItemImport(
  180. filedata, # cert data
  181. None, # Filename, leaving it out for now
  182. None, # What the type of the file is, we don't care
  183. None, # what's in the file, we don't care
  184. 0, # import flags
  185. None, # key params, can include passphrase in the future
  186. keychain, # The keychain to insert into
  187. ctypes.byref(result_array), # Results
  188. )
  189. _assert_no_error(result)
  190. # A CFArray is not very useful to us as an intermediary
  191. # representation, so we are going to extract the objects we want
  192. # and then free the array. We don't need to keep hold of keys: the
  193. # keychain already has them!
  194. result_count = CoreFoundation.CFArrayGetCount(result_array)
  195. for index in range(result_count):
  196. item = CoreFoundation.CFArrayGetValueAtIndex(result_array, index)
  197. item = ctypes.cast(item, CoreFoundation.CFTypeRef)
  198. if _is_cert(item):
  199. CoreFoundation.CFRetain(item)
  200. certificates.append(item)
  201. elif _is_identity(item):
  202. CoreFoundation.CFRetain(item)
  203. identities.append(item)
  204. finally:
  205. if result_array:
  206. CoreFoundation.CFRelease(result_array)
  207. CoreFoundation.CFRelease(filedata)
  208. return (identities, certificates)
  209. def _load_client_cert_chain(keychain, *paths):
  210. """
  211. Load certificates and maybe keys from a number of files. Has the end goal
  212. of returning a CFArray containing one SecIdentityRef, and then zero or more
  213. SecCertificateRef objects, suitable for use as a client certificate trust
  214. chain.
  215. """
  216. # Ok, the strategy.
  217. #
  218. # This relies on knowing that macOS will not give you a SecIdentityRef
  219. # unless you have imported a key into a keychain. This is a somewhat
  220. # artificial limitation of macOS (for example, it doesn't necessarily
  221. # affect iOS), but there is nothing inside Security.framework that lets you
  222. # get a SecIdentityRef without having a key in a keychain.
  223. #
  224. # So the policy here is we take all the files and iterate them in order.
  225. # Each one will use SecItemImport to have one or more objects loaded from
  226. # it. We will also point at a keychain that macOS can use to work with the
  227. # private key.
  228. #
  229. # Once we have all the objects, we'll check what we actually have. If we
  230. # already have a SecIdentityRef in hand, fab: we'll use that. Otherwise,
  231. # we'll take the first certificate (which we assume to be our leaf) and
  232. # ask the keychain to give us a SecIdentityRef with that cert's associated
  233. # key.
  234. #
  235. # We'll then return a CFArray containing the trust chain: one
  236. # SecIdentityRef and then zero-or-more SecCertificateRef objects. The
  237. # responsibility for freeing this CFArray will be with the caller. This
  238. # CFArray must remain alive for the entire connection, so in practice it
  239. # will be stored with a single SSLSocket, along with the reference to the
  240. # keychain.
  241. certificates = []
  242. identities = []
  243. # Filter out bad paths.
  244. paths = (path for path in paths if path)
  245. try:
  246. for file_path in paths:
  247. new_identities, new_certs = _load_items_from_file(keychain, file_path)
  248. identities.extend(new_identities)
  249. certificates.extend(new_certs)
  250. # Ok, we have everything. The question is: do we have an identity? If
  251. # not, we want to grab one from the first cert we have.
  252. if not identities:
  253. new_identity = Security.SecIdentityRef()
  254. status = Security.SecIdentityCreateWithCertificate(
  255. keychain, certificates[0], ctypes.byref(new_identity)
  256. )
  257. _assert_no_error(status)
  258. identities.append(new_identity)
  259. # We now want to release the original certificate, as we no longer
  260. # need it.
  261. CoreFoundation.CFRelease(certificates.pop(0))
  262. # We now need to build a new CFArray that holds the trust chain.
  263. trust_chain = CoreFoundation.CFArrayCreateMutable(
  264. CoreFoundation.kCFAllocatorDefault,
  265. 0,
  266. ctypes.byref(CoreFoundation.kCFTypeArrayCallBacks),
  267. )
  268. for item in itertools.chain(identities, certificates):
  269. # ArrayAppendValue does a CFRetain on the item. That's fine,
  270. # because the finally block will release our other refs to them.
  271. CoreFoundation.CFArrayAppendValue(trust_chain, item)
  272. return trust_chain
  273. finally:
  274. for obj in itertools.chain(identities, certificates):
  275. CoreFoundation.CFRelease(obj)